package com.intelligencesoft.pss.utils;

import javax.servlet.http.HttpServletRequest;

public class AuthUtil {


	/*public static void getAdminForwardUrl(HttpServletRequest request,
			HttpServletResponse response) {
		String requestURL = request.getRequestURL().toString();
		if (requestURL.contains(ADMINPATH)) {
			response.setHeader("Access-Control-Allow-Origin", "*");
//			response.setHeader("P3P","CP=\"CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR\"");
  
			Cookie c = new Cookie(REDIRECTACTION, requestURL);
			c.setPath(COOKIEPATH);
			response.addCookie(c);
		}
	}

	public static void getUserForwardUrl(HttpServletRequest request,
			HttpServletResponse response) {
		StringBuffer paramsBuffer = new StringBuffer();
		Map<String, String[]> paramMap = request.getParameterMap();
		if (null != paramMap) {
			Iterator<String> it = paramMap.keySet().iterator();
			while (it.hasNext()) {
				String key = it.next();
				String[] values = paramMap.get(key);
				if (values.length > 0) {
					paramsBuffer.append(key);
					paramsBuffer.append("=");
					paramsBuffer.append(values[0]);
					paramsBuffer.append("&");
				}
			}
		}
		String requestURL = request.getRequestURL().toString();
		String url = paramsBuffer.length() == 0 ? requestURL : requestURL + "?"
				+ paramsBuffer.substring(0, paramsBuffer.length() - 1);
		response.setHeader("Access-Control-Allow-Origin", "*");
//		response.setHeader("P3P","CP=\"CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR\"");
		Cookie c = new Cookie(REDIRECTACTION, url);
		c.setPath(COOKIEPATH);
		response.addCookie(c);
	}*/
	
	/**
	 * 检查是否应该忽略当前请求
	 * 
	 * @param request
	 *            HttpServletRequest对象
	 * @return 忽略则返回true, 否则返回false
	 */
	public static boolean isExclude(HttpServletRequest request,String[] excludes) {
		String uri = request.getRequestURI();
		// 如果以多个/开始
		if (uri.matches("^/{2,}.*")) {
			uri = uri.replaceAll("^/{2,}", "/");
		}
		if (excludes != null && excludes.length > 0) {
			for (String p : excludes) {
				if (uri.matches(p.trim())) {
					return true;
				}
			}
		}
		return false;
	}
	public static String stringToBuffer(String...source) {
		StringBuffer buffer=new StringBuffer();
		for (String s : source) {
			buffer.append(s);
		}
		return buffer.toString();
	}
	
	/**
	 * 检查是否应该包含当前请求
	 * 
	 * @param request
	 *            HttpServletRequest对象
	 * @return 包含则返回true, 否则返回false
	 */
	/*private boolean isInclude(HttpServletRequest request) {
		String uri = request.getRequestURI();
		// 如果以多个/开始
		if (uri.matches("^/{2,}.*")) {
			uri = uri.replaceAll("^/{2,}", "/");
		}
		if (includes != null && includes.length > 0) {
			for (String p : includes) {
				if (uri.matches(p.trim())) {
					return true;
				}
			}
		}
		return false;
	}*/
}
